package com.buba.controller;

import com.alibaba.fastjson.JSON;
import com.buba.pojo.Employee;
import com.buba.service.EmployeeService;
import com.buba.utils.DataGridViewResult;
import com.buba.utils.MyContent;
import com.buba.utils.SystemConstant;
import com.buba.vo.EmployeeVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("employee")
public class LoginController {
    @Autowired
    EmployeeService employeeService;



    @RequestMapping("login")
    public String login(Employee employee, HttpSession session,Model model){
        Employee emp= employeeService.getEmpByloginnameAndloginpwd(employee);
        if(emp!=null){
            session.setAttribute(MyContent.DATA,emp);
            return "admin/home";
        }else {
            model.addAttribute(MyContent.MSG,"*用户名或密码错误");
            return "admin/login";
        }

    }
    /**
     * todo 员工列表 DataGridViewResult调用表格工具类
     * @param employeeVO
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public DataGridViewResult list(EmployeeVO employeeVO) {
        //设置分页信息
        PageHelper.startPage(employeeVO.getPage(), employeeVO.getLimit());
        //调用查询的方法
        List<Employee> employeeList = employeeService.findEmployeeList(employeeVO);
        //创建分页对象
        PageInfo<Employee> pageInfo = new PageInfo<Employee>(employeeList);
        //返回数据
        return new DataGridViewResult(pageInfo.getTotal(), pageInfo.getList());
    }
    /**
     * todo 添加员工
     * @param employee
     * @param session
     * @return
     */
    @RequestMapping("/addEmployee")
    @ResponseBody
    public String addEmployee(Employee employee, HttpSession session) {

        Map<String, Object> map = new HashMap<String, Object>();
        //获取当前登录用户
        Employee loginUser = (Employee) session.getAttribute(MyContent.DATA);
        //设置创建人
        employee.setCreatedBy(loginUser.getId());
        //调用新增员工的方法
        if (employeeService.addEmployee(employee) > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "添加成功");
        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "添加失败");
        }
        return JSON.toJSONString(map);

    }

    /**
     * todo 修改员工
     *
     * @param employee
     * @param session
     * @return
     */
    @RequestMapping("/updateEmployee")
    @ResponseBody
    public String updateEmployee(Employee employee, HttpSession session) {

        Map<String, Object> map = new HashMap<String, Object>();
        //获取当前登录用户
        Employee loginUser = (Employee) session.getAttribute(MyContent.DATA);
        //设置修改人
        employee.setModifyBy(loginUser.getId());
        //调用修改员工的方法
        if (employeeService.updateEmployee(employee) > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "添加成功");
        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "添加失败");
        }
        return JSON.toJSONString(map);

    }
    /**
     * todo 删除员工
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    @ResponseBody
    public String deleteById(int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        //调用修改员工的方法
        if (employeeService.deleteById(id) > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "删除成功");
        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "删除失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * todo 重置
     * @param id
     * @return
     */
    @RequestMapping("/resetPwd")
    @ResponseBody
    public String resetPwd(int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        //调用密码重置方法
        if (employeeService.resetPwd(id) > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "密码重置成功");
        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "密码重置失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * todo 保存员工角色关系
     * @param roleIds
     * @param empId
     * @return
     */
    @RequestMapping("/saveEmployeeRole")
    @ResponseBody
    public String saveEmployeeRole(String roleIds, Integer empId) {
        Map<String, Object> map = new HashMap<String, Object>();
        //调用保存员工角色关系的方法
        if (employeeService.saveEmployeeRole(roleIds, empId)) {
            map.put(SystemConstant.MESSAGE, "角色分配成功");
        } else {
            map.put(SystemConstant.MESSAGE, "角色分配失败");
        }
        return JSON.toJSONString(map);
    }



}
